var app = getApp();
var Zan = require('../../dist/index');
Page({
  /**
   * 页面的初始数据
   */
  data: {
    show: true,
    linkmanGuid:'',
    // 是否显示loading
    showLoading: false,
    // loading提示语
    loadingMessage: '',
    user: {},
    canSee:false,
  },
  // 显示loading提示
  showLoading(loadingMessage) {
    this.setData({ showLoading: true, loadingMessage });
  },

  // 隐藏loading提示
  hideLoading() {
    this.setData({ showLoading: false, loadingMessage: '' });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      linkmanGuid: options.linkmanGuid
    });
    console.log(this.data.linkmanGuid);
  },
  onShow:function(){
    var self = this;
    this.showLoading('加载中...');
    getData(self);
  },
  modify: function () {
    wx.navigateTo({
      url: "../modifyLinkman/modifyLinkman?linkmanGuid=" + this.data.linkmanGuid
    })
  },
  delete: function () {
    var self = this;
    wx.showModal({
      title: '提示',
      content: '是否确定删除？',
      confirmText: "确定",
      cancelText: "取消",
      success: function (res) {
        console.log(res);
        if (res.confirm) { 
          wx.showNavigationBarLoading();
          deleteLinkman(self);
        } else {
          console.log('用户点击辅助操作')
        }
      }
    });   
  },
copyTBL: function (e) {
    var self = this;
    wx.setClipboardData({
      data: self.data.user.linkmanTelephone,
      success: function (res) {
        // self.setData({copyTip:true}),
        // wx.showModal({
        //   title: '提示',
        //   content: '复制成功',
        //   showCancel:false,
        //   success: function (res) {
            
        //   }
        // })
      }
    });
},
call:function(e){
  let that = this
  var linkmanTelephone = that.data.user.linkmanTelephone;
  console.log(linkmanTelephone);
  wx.makePhoneCall({
    phoneNumber: linkmanTelephone //仅为示例，并非真实的电话号码
  })
},
addtotxl:function(e){
  let that = this
  // 添加到手机通讯录
  wx.addPhoneContact({
    firstName: that.data.user.linkmanName,//联系人姓名
    mobilePhoneNumber: that.data.user.linkmanTelephone,//联系人手机号
  })

}
})

//获取数据
var getData = function (self) {
  wx.request({
    url: app.globalData.ServiceUrl + 'Linkman/getLinkmanByGuid', //接口地址
    method: 'POST',
    data: {
      ValidateData: app.globalData.ValidateData,
      paras: {
        linkmanGuid: self.data.linkmanGuid,
      }
    },
    header: {
      'content-type': 'application/json' // 默认值
    },
    success: function (res) {
      console.log(res.data)
      wx.stopPullDownRefresh();
      self.hideLoading();
      if (res.data.BusinessInfo.Code == 1 && res.data.ReturnInfo.Code == 1) {
          self.setData({
            user: res.data.UserArea,
          });
          var userguid = wx.getStorageSync('OPEN_ID');
          console.log(res.data.UserArea.userGuid)
          console.log(userguid)
          if (res.data.UserArea.userGuid == userguid){
            self.setData({
              canSee: true,
            });
          }
      } else {
        wx.showToast({
          title: '获取数据失败！',
          image: '../../images/img_jinggao.png',
          duration: 1000,
        })
      }
    }
  })
}
//获取数据
var deleteLinkman = function (self) {
  wx.request({
    url: app.globalData.ServiceUrl + 'Linkman/deleteLinkman', //接口地址
    method: 'POST',
    data: {
      ValidateData: app.globalData.ValidateData,
      paras: {
        linkmanGuids: self.data.linkmanGuid,
      }
    },
    header: {
      'content-type': 'application/json' // 默认值
    },
    success: function (res) {
      console.log(res.data)
      wx.stopPullDownRefresh();
      if (res.data.BusinessInfo.Code == 1 && res.data.ReturnInfo.Code == 1) {
        wx.showToast({
          title: '删除成功',
          icon: 'success',
          duration: 3000
        });
        wx.navigateBack({ changed: true });//返回上一页
      } else {
        wx.showToast({
          title: '删除失败！',
          image: '../../images/img_jinggao.png',
          duration: 1000,
        })
      }
    }
  })
}